from PySide6.QtWidgets import QDialog
from sqlalchemy.orm import Session
from views.initsetting.initsetting import Ui_initsetting
from settings.publicvar import engine
from views.initsetting.mode.initsetting import InitSetting
from views.schoolInfo.model.SchoolInfo import SchoolInfo
from views.person.model.person import PersonBaseInfo


class InitSettingView(QDialog,Ui_initsetting):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.setFormStructure()
        self.is_state()
        self.savebtn.clicked.connect(self.saveFun)
        self.changebtn.clicked.connect(self.changeFun)
        self.resetbtn.clicked.connect(self.resetFun)
        # self.name.currentTextChanged.connect(self.changeName)

    def setFormStructure(self):
        with Session(engine) as session,session.begin():
            schools = session.query(SchoolInfo).all()
            names = session.query(PersonBaseInfo).all()
            for school in schools:
                self.schoolinfo.addItem(str(school.id) + "-" + school.name)
            for name in names:
                self.name.addItem(str(name.id) + "-" + name.name)
            try:
                initsetting = session.query(InitSetting).one()
                self.idx.setText(str(initsetting.id))
                name = str(initsetting.name.id)+"-"+initsetting.name.name
                self.name.setCurrentText(name)
                school = initsetting.school
                self.schoolinfo.setCurrentText(str(school.id)+"-"+school.name)
            except:
                self.idx.setText('')

    def is_state(self):
        with Session(engine) as session,session.begin():
            # initsetting = session.query(InitSetting).delete()
            try:
                session.query(InitSetting).one()
                self.savebtn.hide()
            except:
                self.changebtn.hide()

    def saveFun(self):
        name = self.name.currentText()
        school = self.schoolinfo.currentText()
        school_id = school.split('-')[0]
        personbase = name.split('-')[0]
        with Session(engine) as session, session.begin():
            session.query(InitSetting).all()
            initsetting = InitSetting(name_id=personbase,school_id=school_id)
            session.add(initsetting)
            self.close()

    def changeFun(self):
        idx = self.idx.text()
        name = self.name.currentText()
        school = self.schoolinfo.currentText()
        school_id = school.split('-')[0]
        personbase = name.split('-')[0]
        with Session(engine) as session, session.begin():
            session.query(InitSetting).filter(InitSetting.id==idx).update({'name_id':personbase,'school_id':school_id})
        self.close()

    def resetFun(self):
        self.name.setCurrentText('')
        self.schoolinfo.setCurrentText('')

    def changeName(self,name):
        with Session(engine) as session,session.begin():
            # self.name.clear()
            names = session.query(PersonBaseInfo).filter(PersonBaseInfo.name.like('%' + name + '%')).all()
            # print(names)
            # for name in names:
            #     self.name.addItem(str(name.id) + "-" + name.name)